﻿package com.wxsoft.business.physic.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.wxsoft.business.model.easyui.PageHelper;
import com.wxsoft.business.physic.dao.IPsDispensaryStockDao;
import com.wxsoft.business.physic.dao.IPsPhysicDictDao;
import com.wxsoft.business.physic.dao.IPsPhysicInDetailDao;
import com.wxsoft.business.physic.dao.IPsPhysicInHeadDao;
import com.wxsoft.business.physic.pojo.PsDispensaryStock;
import com.wxsoft.business.physic.pojo.PsPhysicDict;
import com.wxsoft.business.physic.pojo.PsPhysicInDetail;
import com.wxsoft.business.physic.pojo.PsPhysicInHead;
import com.wxsoft.business.physic.service.IPsPhysicInService;
import com.wxsoft.util.common.DateUtil;
import com.wxsoft.util.wx.UUIDUtil;

@Service("psPhysicInService")
public class PsPhysicInServiceImpl implements IPsPhysicInService  {
	@Resource
	private IPsPhysicInHeadDao dao;
	@Resource
	private IPsPhysicInDetailDao dtlDao;
	@Resource
	private IPsPhysicDictDao physcDao;
	@Resource
	private IPsDispensaryStockDao stockDao;
	
	public long findCount(PsPhysicInHead psPhysicInHead) {
		return dao.findCount(psPhysicInHead);
	}
	
	public List<PsPhysicInHead> findAll(PageHelper page,PsPhysicInHead psPhysicInHead) {
        List<PsPhysicInHead> r = dao.findAll(page,psPhysicInHead);
		return r;
	}
	public List<PsPhysicInHead> getBy(PsPhysicInHead psPhysicInHead) {
        List<PsPhysicInHead> r = dao.getBy(psPhysicInHead);
		return r;
	}
	@Transactional
	public String add(PsPhysicInHead psPhysicInHead) {
		psPhysicInHead.setId(UUIDUtil.getUUID());
		int r = this.dao.insert(psPhysicInHead);
		if(r==1){
			return "操作成功!";
		}
		return "操作失败!";
	}
	
	public String modify(PsPhysicInHead psPhysicInHead) {
		int r = this.dao.modify(psPhysicInHead);
		if(r==1){
			return "操作成功!";
		}
		return "操作失败!";
	}

	/**
	 * 删除入库单，同时删除明细
	 */
	@Transactional
	public String delete(PsPhysicInHead psPhysicInHead) {
		PsPhysicInDetail ppidQ = new PsPhysicInDetail();
		ppidQ.setInout_order_number(psPhysicInHead.getInout_order_number());
		List<PsPhysicInDetail> ppihL = dtlDao.getBy(ppidQ);
		if(ppihL!=null && ppihL.size()>0){
			delDtl(ppihL,psPhysicInHead);
		}
		int r = this.dao.delete(psPhysicInHead);
		if(r==1){
			
			return "操作成功!";
		}
		return "操作失败!";
	}

	/**
	 * 修改入库单操作
	 */
	@Transactional
	public String modify(PsPhysicInHead psPhysicInHead,List<PsPhysicInDetail> ppidForI,List<PsPhysicInDetail> ppidForU,List<PsPhysicInDetail> ppidForD) {
		//先去需要修改的记录
		PsPhysicInHead ppihQ = new PsPhysicInHead();
		ppihQ.setId(psPhysicInHead.getId());
		List<PsPhysicInHead> ppihL = dao.getBy(ppihQ);
		PsPhysicInHead oldPpih = null ;
		if(ppihL!=null && ppihL.size()>0){
			oldPpih = ppihL.get(0);
		}
		//判断要修改的记录是否已 审核记账
		if("yes".equals(oldPpih.getAccount_sign())){
			throw new RuntimeException(oldPpih.getInout_order_number()+"已审核，不允许修改。");
		}
		//执行修改操作
		int r = this.dao.modify(psPhysicInHead);
		if(r!=1){
			throw new RuntimeException("操作失败!");
		}

		//修改完主记录，赋值 明细中需要的数据
		psPhysicInHead.setAccount_sign(oldPpih.getAccount_sign());
		
		//保存明细
		if(ppidForI!=null){
			addDtl(ppidForI,psPhysicInHead);
		}
		if(ppidForU!=null){
			updateDtl(ppidForU,psPhysicInHead);
		}
		if(ppidForD!=null){
			delDtl(ppidForD,psPhysicInHead);
		}
		
		return "操作成功!";
		
	}
	/**
	 * 添加入库单数据
	 */
	@Transactional
	public String add(PsPhysicInHead psPhysicInHead,
			List<PsPhysicInDetail> ppidForI, List<PsPhysicInDetail> ppidForU,
			List<PsPhysicInDetail> ppidForD) {
//		psPhysicInHead.setInout_order_number(CodeUtil.getCode("io"));

		PsPhysicInHead ppihQ = new PsPhysicInHead();
		ppihQ.setInout_order_number(psPhysicInHead.getInout_order_number());
		//判断
		List<PsPhysicInHead> ppihL = dao.getBy(ppihQ);
		if(ppihL!=null && ppihL.size()>0){
			throw new RuntimeException("已存在单据号"+psPhysicInHead.getInout_order_number()+"，请重新输入。");
		}
		
		//默认没有记账
		psPhysicInHead.setAccount_sign("no");
		//保存头
		psPhysicInHead.setId(UUIDUtil.getUUID());
		int r = this.dao.insert(psPhysicInHead);
		
		//保存明细
		if(ppidForI!=null){
			addDtl(ppidForI,psPhysicInHead);
		}
		if(ppidForU!=null){
			updateDtl(ppidForU,psPhysicInHead);
		}
		if(ppidForD!=null){
			delDtl(ppidForD,psPhysicInHead);
		}
		
		return "保存成功";
	}
	/**
	 * 添加明细数据
	 * @Description:TODO
	 * @author: chenliang
	 * @time:2018-3-22 上午9:00:49
	 */
	@Transactional
	public String addDtl(List<PsPhysicInDetail> l,PsPhysicInHead psPhysicInHead){
		if(l!=null){
			for(PsPhysicInDetail ppid : l){
				PsPhysicDict ppd = physcDao.getByCode(ppid.getPhysic_code());
				ppid.setDepartment(psPhysicInHead.getUnit_id());
				ppid.setHandle(psPhysicInHead.getHandle());
				ppid.setHandle_date(psPhysicInHead.getHandle_date());
				
				ppid.setAccount_sign(psPhysicInHead.getAccount_sign());
//				ppid.setAccount_sign("no");
				ppid.setInout_order_number(psPhysicInHead.getInout_order_number());
				//药品标准编码
				ppid.setPhysic_std_code(ppd.getPhysic_code());
				//生成厂家
				ppid.setPhysic_factory_code(ppd.getManufactor_code());
				ppid.setPhysic_name_code(ppd.getPhysic_name_code());
				//有效期
//				ppid.setValid_period(new Date());
				ppid.setPhysic_form(ppd.getPhysic_form());
				ppid.setPhysic_spec(ppd.getPhysic_spec());
				ppid.setPhysic_unit(ppd.getPhysic_unit());
				ppid.setId(UUIDUtil.getUUID());
				
				dtlDao.insert(ppid);
			}
		}
		
		return "";
	}
	/**
	 * 修改明细数据
	 * @Description:TODO
	 * @author: chenliang
	 * @time:2018-3-22 上午9:01:05
	 */
	@Transactional
	public String updateDtl(List<PsPhysicInDetail> l,PsPhysicInHead psPhysicInHead){

		if(l!=null){
			for(PsPhysicInDetail ppid : l){
				dtlDao.modify(ppid);
			}
		}
		
		return "";
	}
	/**
	 * 删除明细数据
	 * @Description:TODO
	 * @author: chenliang
	 * @time:2018-3-22 上午9:01:23
	 */
	@Transactional
	public String delDtl(List<PsPhysicInDetail> l,PsPhysicInHead psPhysicInHead){

		if(l!=null){
			for(PsPhysicInDetail ppid : l){
				dtlDao.delete(ppid);
			}
		}
		
		return "";
	}

	/**
	 * 审核记账操作
	 */
	@Transactional
	public String examine(PsPhysicInHead psPhysicInHead) {
		
		PsPhysicInHead ppihQ = new PsPhysicInHead();
		ppihQ.setInout_order_number(psPhysicInHead.getInout_order_number());
		//判断是否已经审核
		List<PsPhysicInHead> ppihL = dao.getBy(ppihQ);
		if(ppihL!=null && ppihL.size()==1){
			PsPhysicInHead ppih = ppihL.get(0);
			String account_sign = ppih.getAccount_sign();
			//判断审核标志为已审核
			if("yes".equals(account_sign)){
				return psPhysicInHead.getContract_order_number()+"此条入库单，已审核";
			}
		}else{
			throw new RuntimeException(psPhysicInHead.getContract_order_number()+"有多条，请联系管理员！");
		}
		
		//更新 审核人、审核日期、是否记账 已记账、
		int r = dao.modify(psPhysicInHead);
		if(r!=1){
			throw new RuntimeException("更新单据头错误");
		}
		
		//更新库存操作 
		//先根据 单号 查询出 单据明细
		PsPhysicInDetail psPhysicInDetail = new PsPhysicInDetail();
		psPhysicInDetail.setInout_order_number(psPhysicInHead.getInout_order_number());
		List<PsPhysicInDetail> ppidL = dtlDao.getBy(psPhysicInDetail);
		for(PsPhysicInDetail ppid : ppidL){
			updateStock(ppid);
		}
		return "操作成功!";
	}

	/**
	 * 根据入库明细 更新 库存 
	 * @Description:TODO
	 * @author: chenliang
	 * @time:2018-3-22 下午10:36:06
	 */
	@Transactional
	public String updateStock(PsPhysicInDetail ppid){
		PsDispensaryStock pds =  new PsDispensaryStock();
		
		pds.setUnit_id(ppid.getDepartment());
		pds.setPhysic_code(ppid.getPhysic_code());
		//查询这个药品是否存在库存
		List<PsDispensaryStock> oldPdsL = stockDao.getBy(pds);
		if(oldPdsL!=null && oldPdsL.size()>0){	//已存在则更新库存
			PsDispensaryStock oldPds = oldPdsL.get(0);
			oldPds.setPhysic_std_code(ppid.getPhysic_std_code());
			oldPds.setIn_price(ppid.getIn_price());
			oldPds.setTrade_price(ppid.getTrade_price());
			oldPds.setRetail_price(ppid.getRetail_price());
			oldPds.setLast_update(new Date());
			oldPds.setValidity(DateUtil.format(ppid.getValid_period()));
			
			BigDecimal quantity = ppid.getQuantity();	//入库数量
			BigDecimal currStock = oldPds.getQuantity();	//当前库存数量
			oldPds.setQuantity(currStock.add(quantity));	//新的库存数量
			oldPds.setTemp_quantity(currStock.add(quantity));
			
			stockDao.modify(oldPds);
			
		}else{	//不存在插入库存
			PsDispensaryStock newPds =  new PsDispensaryStock();

			newPds.setId(UUIDUtil.getUUID());
			newPds.setUnit_id(ppid.getDepartment());
			newPds.setPhysic_code(ppid.getPhysic_code());
			
			newPds.setPhysic_std_code(ppid.getPhysic_std_code());
			newPds.setIn_price(ppid.getIn_price());
			System.out.println(ppid.getTrade_price());
			newPds.setTrade_price(ppid.getTrade_price()==null?new BigDecimal(0):ppid.getTrade_price());
			newPds.setRetail_price(ppid.getRetail_price()==null?new BigDecimal(0):ppid.getRetail_price());
			newPds.setLast_update(new Date());
			newPds.setValidity(DateUtil.format(ppid.getValid_period()));
			newPds.setQuantity(ppid.getQuantity());	//新的库存数量
			newPds.setTemp_quantity(ppid.getQuantity());

			stockDao.insert(newPds);
			
		}
		
		return "";
	}
	
}
